package com.ltb.speedlottery.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter
{
	protected LayoutInflater mInflater;
	protected Context mContext;
	protected List<T> mDatas;
	protected final int mItemLayoutId;
	protected int mSelectedPositioin = -1;
	protected TextView mClickView;
	public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId)
	{
		this.mContext = context;
		this.mInflater = LayoutInflater.from(mContext);
		this.mDatas = mDatas;
		this.mItemLayoutId = itemLayoutId;
	}

	@Override
	public int getCount()
	{
		return mDatas.size();
	}

	@Override
	public T getItem(int position)
	{
		if(mDatas == null || mDatas.size() <=0){
			return null;
		}
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		final ViewHolder viewHolder = getViewHolder(position, convertView,
				parent);
		convert(viewHolder, getItem(position), position);
		return viewHolder.getConvertView();

	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	public abstract void convert(ViewHolder helper, T item, int position);

	private ViewHolder getViewHolder(int position, View convertView,
			ViewGroup parent)
	{
		return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
				position);
	}

	public int getSelectedPositioin() {
		return mSelectedPositioin;
	}

	public void setSelectedPositioin(int selectedPositioin) {
		this.mSelectedPositioin = selectedPositioin;
	}
	
	public TextView getClickView() {
		return mClickView;
	}

	public void setClickView(TextView textView) {
		this.mClickView = textView;
	}
	
	public void update(List<T> list){
		if(list == null){
			return;
		}
		mDatas.clear();
		mDatas.addAll(list);
		notifyDataSetChanged();
	}
}
